<!--title-->
<div class="mnav1">
	<div class="m_nav">
		<div class="nav_left">
			<em class="home_icon"><?=$this->sys_name?></em><span>&nbsp;&gt;&nbsp;</span><font><?=$this->title?></font>
		</div>
	</div>
</div>

<div class="content-table">
<div class="content-table-title"><?=$this->title?></div>
<div class="form-edit-task2">
<br>
<form action="/contracts/add-dealers-into-contract-submit" method="post" name="form1" onsubmit="submitform(this);return false;">
<div align="center">输入关键字搜索经销商：&nbsp;&nbsp;<input type="text" placeholder="经销商关键字" name="keyword" id="keyword" style="width: 300px" onkeyup="Search(this)"></div>
<br /><input type="hidden" name="dealer_id" id="dealer_id" value="" />
<table border="0" width="100%" id="dealer_list"></table>
<br /><br />
<div align="center" id="select_button"></div>
<?php if($this->contract_id):?>
<input type="hidden" name="contract_id" value="<?=$this->contract_id?>" />
<?php endif;?>
</form>
<br><br><br>
<table style="width: 600px" align="center">
	<tr>
		<th colspan="3">已选择的经销商</th>
	</tr>
	<?php
		if($this->contract_id)
		{
			$target_session = $_SESSION['contract-edit-type2'][$this->contract_id];
			$return_url = "/contracts/edit-contract-type2/contract_id/".$this->contract_id;
		}else{
			$target_session = $_SESSION['contract'];
			$return_url = "/contracts/add-contract-step2";
		}
	?>
	<?php if(!empty($target_session['contract_info']['dealers_arr'])):?>
	<?php foreach($target_session['contract_info']['dealers_arr'] as $dealer_id => $dealer_name):?>
	<tr>
		<td><?=$dealer_id?></td>
		<td><?=$dealer_name?></td>
		<td><a onclick="Del(<?=$dealer_id?>)">删除</a></td>
	</tr>
	<?php endforeach;?>
	<?php endif;?>
</table>
</div>
<div align="center"><input type="button" class="buttonlong" value="添加完毕，返回合同" onclick="window.location.href='<?=$return_url?>'" /></div>
</div>
<br/><br/>
</div>

<script>
try{
	document.getElementById('keyword').focus();
	document.getElementById('keyword').select();
	}
catch(e){}

function Search(keyword)
{
	var kw = keyword.value;
	
	$.ajax({
			type: "post",
			url : "/ajax/get-dealer-list",
			dataType:'json',
            async:false,
			data: {
					"kw":kw
				  },
				success: function(json){
					if(json.result == 1 && typeof(json.data) != "undefined") {
                        var arr = json.data;
						var txt = "";
						
                        for(i=0; i < arr.length; i++){
							txt += "<tr><td width='40%' align='right' style='border: 0px'><input type='radio' name='dealer_id' id='dealer_id_" + arr[i]['id'] + "' value='" + arr[i]['id'] + "'></td><td align='left' style='border: 0px'><label for='dealer_id_" + arr[i]['id'] + "'>&nbsp;&nbsp;&nbsp;&nbsp;" + arr[i]['name'] + "</label></td></tr>";
                        }
						
						document.getElementById("dealer_list").innerHTML = txt;
						
						document.getElementById("select_button").innerHTML = '<input type="button" class="buttonlong" value="选择此经销商" onclick="submitform();return false;" />';
                    }
			}
		});
}

function submitform() 
{
	if( document.form1.dealer_id.value.length == 0 )
	{
		alert("请选择一个经销商");
		return false;
	}
	
	form1.submit();
}

function Del(dealer_id)
{
	var msg = "确定要删除吗？"; 
	if (confirm(msg)==true){
		window.location.href='/contracts/del-dealer-from-contract/dealer_id/' + dealer_id + '/contract_id/<?=$this->contract_id?$this->contract_id:""?>';
	}else{ 
		return false; 
	} 
}
</script>
